package com.lmj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.lmj.dto.DataRequest;
import com.lmj.dto.DataResponse;
import com.lmj.dto.HandDictionaryDTO;
import com.lmj.mapper.HandDictionaryMapper;
import com.lmj.pojo.HandDictionary;
import com.lmj.service.HandDictionaryService;
import com.lmj.utils.RequestBody;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HandDictionaryServiceImpl extends ServiceImpl<HandDictionaryMapper, HandDictionary> implements HandDictionaryService {

    @Resource
    private HandDictionaryMapper handDictionaryMapper;

    // 响应体
    private DataResponse dataResponse;
    // 响应数据
    private Map<String, String> map = new HashMap<>();

    @Override
    public DataResponse getHandDictionarys(String request) {

        LambdaQueryWrapper<HandDictionary> queryWrapper = new LambdaQueryWrapper<>();
        List<HandDictionary> list = handDictionaryMapper.selectList(queryWrapper);

        map.clear();

        for (HandDictionary handDictionary : list) {
            HandDictionaryDTO handDictionaryDTO = new HandDictionaryDTO(
                    handDictionary.getHandDictionaryId(),
                    handDictionary.getHandDictionaryName(),
                    handDictionary.getHandDictionaryCountry(),
                    handDictionary.getHandDictionaryWords()
            );
            // 将handDictionaryDTO转化为GSON字符串
            Gson gson = new Gson();
            String handDictionaryStr = gson.toJson(handDictionaryDTO);

            map.put(handDictionaryDTO.getHandDictionaryId().toString(), handDictionaryStr);
        }

        return new DataResponse("200", map);
    }

    public DataResponse getHandDictionary(String request) {
        DataRequest requestBody = RequestBody.getRequestBody(request);
        String handDictionaryId = requestBody.getString("handDictionaryId");
        HandDictionary handDictionary = handDictionaryMapper.selectById(handDictionaryId);

        map.clear();

        map.put("handDictionaryName", handDictionary.getHandDictionaryName().toString());
        map.put("handDictionaryWords", handDictionary.getHandDictionaryWords().toString());

        return new DataResponse("200", map);
    }

}
